package JDBC工具包;

import org.junit.Test;
import 数据库连接.jdbc_test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @作者:侯国威
 * @时间: 2020/4/14/15:10
 * @邮箱:1045077189@qq.com
 */
public class JDBCPackage {
    /**
    *@作者:侯国威
    *@时间：2020/4/15_11:43
    *@描述:链接工具
    */

    public static Connection getconnection() throws IOException, ClassNotFoundException, SQLException {

        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");

        Properties properties = new Properties();
        assert is != null;
        properties.load(is);
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String dirver = properties.getProperty("dirver");
        //加载驱动
        Class.forName(dirver);
        //获取连接
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("连接成功");
        return connection;
    }

    public static void closeResource(Connection connection, PreparedStatement preparedStatement) {

        try {
            if (preparedStatement != null) {
                preparedStatement.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
    /**
    *@作者:侯国威
    *@时间：2020/4/15_1:45
    *@描述: 关闭资源操作，针对查询
    */
    public static void closeResourceInsert(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
        try {
            if (preparedStatement != null) {
                preparedStatement.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (resultSet != null) {
                resultSet.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    }
